#!/bin/sh

# gui package manager for Grafpup Linux
# May 2008 by Nathan Fisher for Grafpup Linux
# This program is free software released under the GNU public license version 2

#########################
# force run as root
#
if [ ! "`whoami`" = "root" ];then
 echo "$LOC_ROOT"
 exec su -c $0
fi

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

#######################################################################
#
# Begin with globals
#
PKGINFODIR="/var/log/packages"
PKGDB="${PKGINFODIR}/pkgdb.txt"
CACHE="${PKGINFODIR}/cache"
REPOLIST="${PKGINFODIR}/repositories.txt"
SPECDIR="${PKGINFODIR}/spec"
FILESDIR="${PKGINFODIR}/files"
CDLIST="${PKGINFODIR}/cdpkgs.txt"
export VERSION="`cat $APPDIR/AppInfo.xml | grep '<Version>' | cut -f 2 -d '>' | cut -f 1 -d '<'`"

# source the configuration file
. $APPDIR/gpkg.conf

# Get translations
if [ $LANGUAGE = auto ]; then
	ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
	case $ENVIRONMENT_LANG in
		de)	export LANGUAGE="german" ;;
		en)	export LANGUAGE="english" ;;
		es)	export LANGUAGE="spanish" ;;
		fr)	export LANGUAGE="french" ;;
		it)	export LANGUAGE="italian" ;;
		nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
		nl)	export LANGUAGE="dutch" ;;
		nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
		ru)	export LANGUAGE="russian" ;;
		*)	export LANGUAGE="english" ;; #english for contries without translation
	esac
fi
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

# Whether or not to show description
if [ "$SHOW_DESC" = "true" ];then
 DESCR="$LOC_DESC"
 TREE_WIDTH="760"
else
 DESCR=""
 TREE_WIDTH="450"
fi

#
# End global declarations
#
#########################


$APPDIR/functions -buildlist

export GPKG_DLG="
<window title=\"Gpkg-$VERSION - $LOC_TITLE\" icon_name=\"system-installer\">
 <vbox>
  <menubar>
   <menu>
    <menuitem icon=\"gtk-open\">
     <label>$LOC_LOCAL</label>
     <action>$APPDIR/functions -install-local</action>
    </menuitem>
    <menuitem icon=\"gtk-refresh\">
     <label>$LOC_UPDATE</label>
     <action>$APPDIR/functions -update-db</action>
    </menuitem>
    <separator></separator>
    <menuitem icon=\"gtk-preferences\">
     <label>$LOC_OPTIONS</label>
     <action>$APPDIR/functions -prefs</action>
    </menuitem>
    <separator></separator>
    <menuitem icon=\"gtk-quit\">
     <label>$LOC_QUIT</label>
     <action type=\"exit\">$LOC_QUIT</action>
    </menuitem>
    <label>$LOC_FILE</label>
   </menu>
   <menu>
    <menuitem icon=\"gtk-about\">
     <label>$LOC_ABOUT</label>
     <action>$APPDIR/functions -about &</action>
    </menuitem>
    <menuitem icon=\"gtk-help\">
     <label>$LOC_HELP</label>
     <action>$APPDIR/functions -help &</action>
    </menuitem>
    <label>$LOC_HELP</label>
   </menu>
  </menubar>
  <hbox>
   <tree>
    <width>170</width>
    <label>$LOC_CATEGORIES</label>
    <variable>CATEGORY</variable>
    <item icon=\"grafpup\">ALL</item>
    <item icon=\"emblem-important\">base</item>
    <item icon=\"utilities-terminal\">console</item>
    <item icon=\"preferences-system-windows\">desktop</item>
    <item icon=\"applications-development\">development</item>
    <item icon=\"help-browser\">doc</item>
    <item icon=\"applications-games\">games</item>
    <item icon=\"applications-graphics\">graphics</item>
    <item icon=\"preferences-desktop-locale\">language</item>
    <item icon=\"application-x-executable\">libraries</item>
    <item icon=\"applications-multimedia\">media</item>
    <item icon=\"network-server\">net</item>
    <item icon=\"applications-office\">office</item>
    <item icon=\"applications-system\">system</item>
    <item icon=\"video-display\">X11</item>
    <action signal=\"cursor_changed\">$APPDIR/functions -view-category \$CATEGORY</action>
    <action signal=\"cursor_changed\" type=\"refresh\">AV_PKGS</action>
    <action signal=\"cursor_changed\" type=\"refresh\">ON_PKGS</action>
    <action signal=\"cursor_changed\">cp -f ${CACHE}/gpkg-on-\${CATEGORY}.txt ${CACHE}/gpkg-search-on.txt</action>
    <action signal=\"cursor_changed\">cp -f ${CACHE}/gpkg-list-\${CATEGORY}.txt ${CACHE}/gpkg-search-av.txt</action>
   </tree>
   <vbox>
    <notebook labels=\"$LOC_AV|$LOC_ON|$LOC_INSTALLQUEUE|$LOC_REMOVEQUEUE\">
     <vbox>
      <tree>
       <height>370</height>
       <width>$TREE_WIDTH</width>
       <variable>AV_PKGS</variable>
       <label>$LOC_NAME	|${DESCR}$LOC_SIZE</label>
       <input icon_column=\"0\">cat ${CACHE}/gpkg-list.txt</input>
       <action>$APPDIR/functions -add-to-queue \$AV_PKGS</action>
       <action type=\"refresh\">INSTALLQUEUE</action>
      </tree>
      <hbox>
       <entry activates-default=\"true\">
        <variable>SEARCH_AV</variable>
       </entry>
       <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
        <input file icon=\"system-search\"></input>
        <label>$LOC_SEARCH</label>
        <action>$APPDIR/functions -search-av \$SEARCH_AV</action>
        <action type=\"refresh\">AV_PKGS</action>
       </button>
       <button>
        <input file stock=\"gtk-info\"></input>
        <action>$APPDIR/functions -showinfo \$AV_PKGS &</action>
       </button>
       <button>
        <input file stock=\"gtk-add\"></input>
        <action>$APPDIR/functions -add-to-queue \$AV_PKGS</action>
        <action type=\"refresh\">INSTALLQUEUE</action>
       </button>
       <button>
        <label>$LOC_APPLY</label>
        <input file stock=\"gtk-refresh\"></input>
        <action>$APPDIR/functions -apply</action>
        <action>cp -f ${CACHE}/gpkg-on-\${CATEGORY}.txt ${CACHE}/gpkg-search-on.txt</action>
        <action>cp -f ${CACHE}/gpkg-list-\${CATEGORY}.txt ${CACHE}/gpkg-search-av.txt</action>
        <action>$APPDIR/functions -view-category \$CATEGORY</action>
        <action type=\"refresh\">AV_PKGS</action>
        <action type=\"refresh\">ON_PKGS</action>
        <action type=\"refresh\">INSTALLQUEUE</action>
        <action type=\"refresh\">REMOVEQUEUE</action>
       </button>
       <button>
        <input file stock=\"gtk-quit\"></input>
        <label>$LOC_QUIT</label>
       </button>
      </hbox>
     </vbox>
     <vbox>
      <tree>
       <variable>ON_PKGS</variable>
       <label>$LOC_NAME		|${DESCR}$LOC_SIZE</label>
       <input icon_column=\"0\">cat /var/log/packages/cache/gpkg-on.txt</input>
       <action>$APPDIR/functions -slot-removal \$ON_PKGS</action>
       <action type=\"refresh\">REMOVEQUEUE</action>
      </tree>
      <hbox>
       <entry activates-default=\"true\">
        <variable>SEARCH_ON</variable>
       </entry>
       <button can-default=\"true\" has-default=\"true\" use-stock=\"true\">
        <input file icon=\"system-search\"></input>
        <label>$LOC_SEARCH</label>
        <action>$APPDIR/functions -search-on \$SEARCH_ON</action>
        <action type=\"refresh\">ON_PKGS</action>
       </button>
       <button>
        <input file stock=\"gtk-info\"></input>
        <action>$APPDIR/functions -showinfo \$ON_PKGS &</action>
       </button>
       <button>
        <input file stock=\"gtk-remove\"></input>
        <action>$APPDIR/functions -slot-removal \$ON_PKGS</action>
        <action type=\"refresh\">REMOVEQUEUE</action>
       </button>
       <button>
        <label>$LOC_APPLY</label>
        <input file stock=\"gtk-refresh\"></input>
        <action>$APPDIR/functions -apply</action>
        <action>cp -f ${CACHE}/gpkg-on-\${CATEGORY}.txt ${CACHE}/gpkg-search-on.txt</action>
        <action>cp -f ${CACHE}/gpkg-list-\${CATEGORY}.txt ${CACHE}/gpkg-search-av.txt</action>
        <action>$APPDIR/functions -view-category \$CATEGORY</action>
        <action type=\"refresh\">AV_PKGS</action>
        <action type=\"refresh\">ON_PKGS</action>
        <action type=\"refresh\">INSTALLQUEUE</action>
        <action type=\"refresh\">REMOVEQUEUE</action>
       </button>
       <button>
        <input file stock=\"gtk-quit\"></input>
        <label>$LOC_QUIT</label>
       </button>
      </hbox>
     </vbox>
     <vbox>
      <tree>
       <label>$LOC_NAME		|${DESCR}$LOC_SIZE</label>
       <variable>INSTALLQUEUE</variable>
       <input icon_column=\"0\">cat ${CACHE}/gpkg-installqueue.txt</input>
      </tree>
      <hbox>
       <entry sensitive=\"false\"></entry>
       <button sensitive=\"false\">
        <input file icon=\"system-search\"></input>
        <label>$LOC_SEARCH</label>
       </button>
       <button>
        <input file stock=\"gtk-info\"></input>
        <action>$APPDIR/functions -showinfo \$INSTALLQUEUE &</action>
       </button>
       <button>
        <input file stock=\"gtk-remove\"></input>
        <action>cat ${CACHE}/gpkg-installqueue.txt | grep -v \$INSTALLQUEUE > ${CACHE}/gpkg-installqueue.new</action>
        <action>mv -f ${CACHE}/gpkg-installqueue.new ${CACHE}/gpkg-installqueue.txt</action>
        <action type=\"removeselected\">INSTALLQUEUE</action>
       </button>
       <button>
        <label>$LOC_APPLY</label>
        <input file stock=\"gtk-refresh\"></input>
        <action>$APPDIR/functions -apply</action>
        <action>cp -f ${CACHE}/gpkg-on-\${CATEGORY}.txt ${CACHE}/gpkg-search-on.txt</action>
        <action>cp -f ${CACHE}/gpkg-list-\${CATEGORY}.txt ${CACHE}/gpkg-search-av.txt</action>
        <action>$APPDIR/functions -view-category \$CATEGORY</action>
        <action type=\"refresh\">AV_PKGS</action>
        <action type=\"refresh\">ON_PKGS</action>
        <action type=\"refresh\">INSTALLQUEUE</action>
        <action type=\"refresh\">REMOVEQUEUE</action>
       </button>
       <button>
        <input file stock=\"gtk-quit\"></input>
        <label>$LOC_QUIT</label>
       </button>
      </hbox>
     </vbox>
     <vbox>
      <tree>
       <label>$LOC_NAME		|${DESCR}$LOC_SIZE</label>
       <variable>REMOVEQUEUE</variable>
       <input icon_column=\"0\">cat ${CACHE}/gpkg-removequeue.txt</input>
      </tree>
      <hbox>
       <entry sensitive=\"false\"></entry>
       <button sensitive=\"false\">
        <input file icon=\"system-search\"></input>
        <label>$LOC_SEARCH</label>
       </button>
       <button>
        <input file stock=\"gtk-info\"></input>
        <action>$APPDIR/functions -showinfo \$REMOVEQUEUE &</action>
       </button>
       <button>
        <input file stock=\"gtk-remove\"></input>
        <action>cat ${CACHE}/gpkg-removequeue.txt | grep -v \$REMOVEQUEUE > ${CACHE}/gpkg-removequeue.new</action>
        <action>mv -f ${CACHE}/gpkg-removequeue.new ${CACHE}/gpkg-removequeue.txt</action>
        <action type=\"removeselected\">REMOVEQUEUE</action>
       </button>
       <button>
        <label>$LOC_APPLY</label>
        <input file stock=\"gtk-refresh\"></input>
        <action>$APPDIR/functions -apply</action>
        <action>cp -f ${CACHE}/gpkg-on-\${CATEGORY}.txt ${CACHE}/gpkg-search-on.txt</action>
        <action>cp -f ${CACHE}/gpkg-list-\${CATEGORY}.txt ${CACHE}/gpkg-search-av.txt</action>
        <action>$APPDIR/functions -view-category \$CATEGORY</action>
        <action type=\"refresh\">AV_PKGS</action>
        <action type=\"refresh\">ON_PKGS</action>
        <action type=\"refresh\">INSTALLQUEUE</action>
        <action type=\"refresh\">REMOVEQUEUE</action>
       </button>
       <button>
        <input file stock=\"gtk-quit\"></input>
        <label>$LOC_QUIT</label>
       </button>
      </hbox>
     </vbox>
    </notebook>
   </vbox>
  </hbox>
 </vbox>
</window>
"
X1PID="`cat ${CACHE}/splashpid.txt`"
[ ! "$X1PID" = "" ] && kill $X1PID
gtkdialog3 --program GPKG_DLG > /dev/null
